CC=gcc
OBJ=lsq
IMAGE=output.eps
REF=references.bib
DATA=testdata.txt
GENDATA=$(OBJ)_data
OBJS=$(OBJ).c

all: $(OBJ) $(GENDATA) $(DATA) $(OBJ).pdf

$(OBJ): src/$(OBJ).c
	$(CC) -o $@ $<

$(GENDATA): src/$(GENDATA).c
	$(CC) -o $@ $<

$(DATA): $(GENDATA)
	./$(GENDATA)
	./$(OBJ)

$(IMAGE): $(DATA)
	gnuplot $(OBJ).gp
	mv ./$(IMAGE) ./doc

$(OBJ).pdf: doc/$(OBJ).tex $(IMAGE) doc/$(REF)
	cd doc && xelatex $(OBJ)
	cd doc && bibtex $(OBJ)
	cd doc && xelatex $(OBJ)
	cd doc && xelatex $(OBJ)

$(OBJ)_slide.pdf: doc/$(OBJ)_slide.tex doc/$(IMAGE)
	xelatex $(OBJ)_slide
clean:
	rm -rf *.aux *.bbl *.blg *.log *.out *.pdf *.toc \
		$(OBJ) $(GENDATA) $(DATA) $(IMAGE) \
		*.nav *.snm *.dat
	cd doc && rm -rf *.aux *.bbl *.blg *.log *.out *.pdf *.toc *.nav *.snm *.dat $(OBJ) $(GENDATA) $(DATA) $(IMAGE)

exe:
	$(CC) -o $(OBJ) src/$(OBJS)

doc:
	$(CC) -o $(GENDATA) src/$(GENDATA).c
	./$(GENDATA)
	./$(OBJ)
	gnuplot $(OBJ).gp
	mv ./$(IMAGE) ./doc/	
	cd doc && xelatex $(OBJ)
	cd doc && bibtex $(OBJ)
	cd doc && xelatex $(OBJ)
	cd doc && xelatex $(OBJ)
	cd doc && xelatex $(OBJ)_slide

.PHONY: all clean exe doc
